package com.baiyinet.rest;


import android.content.SharedPreferences;

import com.baiyinet.Constants;
import com.baiyinet.biz.AdInfo;
import com.google.gson.JsonObject;

import java.io.Serializable;
import java.util.Map;

import okhttp3.RequestBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author J.Huang
 * @version 1.0
 * @since 16/11/23
 */

public class ApiHelper{

    private Api mApi;
    private SharedPreferences mSharedPref;
    private String mRegId;

    public ApiHelper(Api api, SharedPreferences sharedPref, String regId) {
        mApi = api;
        mSharedPref = sharedPref;
        mRegId = regId;
    }

    public void getAppInfo(Subscriber<AppInfo> subscriber) {
        mApi.getAppInfo(mSharedPref.getString(Constants.KEY_COOKIE, ""), "Android", mRegId)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getLink(Subscriber<LinkStructure> subscriber) {
        mApi.getLink(mSharedPref.getString(Constants.KEY_COOKIE, ""), "Android", mRegId)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getPayParam(Subscriber<PayParam> subscriber, String attachData, String payMethod) {
        mApi.getPayParam(mSharedPref.getString(Constants.KEY_COOKIE, ""), attachData, payMethod)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void uploadImg(Subscriber<UploadImgResponse> subscriber, Map<String, RequestBody> partMap) {
        mApi.uploadImg(mSharedPref.getString(Constants.KEY_COOKIE, ""), partMap)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void downloadImg(Subscriber<DownloadImgResponse> subscriber, Map<String, String> params) {
        mApi.downloadImg(mSharedPref.getString(Constants.KEY_COOKIE, ""), params)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void startAd(Subscriber<AdInfo> subscriber) {
        mApi.startAd(mSharedPref.getString(Constants.KEY_COOKIE, ""))
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void readAll(Subscriber<ChannelInfo> subscriber) {
        mApi.readAll(mSharedPref.getString(Constants.KEY_COOKIE, ""))
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getDetail(Subscriber<PlayerDetail> subscriber, int radioId) {
        mApi.getDetail(mSharedPref.getString(Constants.KEY_COOKIE, ""), radioId)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void addMessage(Subscriber<ApiResult> subscriber, int radioId, String content) {
        mApi.addMessage(mSharedPref.getString(Constants.KEY_COOKIE, ""), content, radioId)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void readMessage(Subscriber<MessageInfo> subscriber, int radioId, int pageNo, int pageSize) {
        mApi.readMessage(mSharedPref.getString(Constants.KEY_COOKIE, ""), radioId, pageNo, pageSize)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

}
